//
//  CFRMRecipeHistoryController.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import UIKit

class CFRMRecipeHistoryController: CFRMBaseViewController {
    @IBOutlet var tableView: UITableView!
    @IBOutlet var infoLabel: UILabel!
    
    lazy var viewModel: CFRMRecipeHistoryViewModel = {
        return CFRMRecipeHistoryViewModel()
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getPageData()

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Fat loss history"
        setTab()
        // Do any additional setup after loading the view.
    }
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "CFRMRecipeHistoryCell", bundle: .main), forCellReuseIdentifier: "CFRMRecipeHistoryCell")
    }
    func getPageData(){
        self.viewModel.loadData { [self] models in
            let firstModel = models.first
            let lastModel = models.last
            self.infoLabel.text = "First record to date weight: \(String(format: "%@", firstModel!.weight))→\(String(format: "%@", lastModel!.weight)) Body fat percentage: \(String(format: "%@", firstModel!.percentage))→\(String(format: "%@", lastModel!.percentage))"
            self.tableView.reloadData()
        }
    }
    
    
}
extension CFRMRecipeHistoryController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:CFRMRecipeHistoryCell = tableView.dequeueReusableCell(withIdentifier: "CFRMRecipeHistoryCell") as! CFRMRecipeHistoryCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 145
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
